<?php
namespace Admin\Model;
use Think\Model;
//获取数据模型
class DataModel extends Model
{
    /*
     * 获取菜单数据
     * 参数：$page 数据分页 一页多少条数据
     */
    public function getMenu($p = '')
    {
        if ($p == '') {
            $data['menu'] = M('Menu')->order('id desc')->select();
        } else {
            $count = M('Menu')->count();
            $page = new \Think\Page($count, $p);
            $data['menu'] = M('Menu')->limit($page->firstRow . ',' . $page->listRows)->order('id desc')->select();
            $data['page'] = $page->show();
        }
        return $data;
    }

    /*
   * 获取顶级菜单数据
   */
    public function getLevelMenu()
    {
        $menu = M('Menu')->where(array('hide' => 0, 'pid' => 0))->select();
        return $menu;
    }


    /*获取代理
     * 参数   $p=>分页  $pid => 父ID
     * */
    public function getMember($p=1,$pid=''){
        $map = array('p1'=>$pid);
        $search = I('get.search');
        if($search) {
            $map['username'] = array('like',"%{$search}");
            $where['username'] = array('like',"%{$search}");
        }  
        $count = M('Member')->where($map)->count();
        $page = new \Think\Page($count, $p);
        if(empty($pid)){
            $data['member'] = M('Member')
                ->alias('m')
                ->join('__GROUP__ g ON g.id = m.group_id')
                ->field('m.*,g.group_name')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->order('id desc')
                ->where($where)
                ->select();
            $data['page'] = $page->show();
        }else{
            $data['member'] = M('Member')
                ->alias('m')
                ->join('__GROUP__ g ON g.id = m.group_id')
                ->field('m.*,g.group_name')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->where($map)
                ->limit($page->firstRow . ',' . $page->listRows)
                ->order('id desc')
                ->select();
            $data['page'] = $page->show();
        }
        return $data;
    }

    //单个查询代理
    /*
    public function member_search($username='',$agentid=''){
        if(!empty($username)){
            $member = M('Member')->where(array('username'=>$username))->find();
            if($member){
                return $member;
            }else{
                $this->error='代理名称不正确';
                return false;
            }
        }elseif(!empty($agentid)){
            $member = M('Member')->where(array('agentid'=>$agentid))->find();
            if($member){
                return $member;
            }else{
                $this->error='代理ID号不正确';
                return false;
            }
        }else{
            $this->error='未传入查询条件';
            return false;
        }
    }
    */

}
